﻿package jokers {
	import jokers.*;
	import flash.display.MovieClip;
	
	public class JTool {
		public static const TOOL_NUM:int = 7;
		
		//放大镜
		public static const TOOL_SEARCH:int = 0;
		//刷新
		public static const TOOL_REFRESH:int = 1;
		//炸弹
		public static const TOOL_BOMB:int = 2;
		//闪电
		public static const TOOL_LIGHTENING:int = 3;
		//魔方
		public static const TOOL_CUBE:int = 4;
		//月光
		public static const TOOL_MOONLIGHT:int = 5;
		//时间
		public static const TOOL_TIME:int = 6;
		//油漆桶
		public static const TOOL_PAINTBUCKET:int = 7;

		
		
		public static function initToolBar():void{
			JPara.toolEnable = "000000000000";
		}
		
		//设置道具等级，0为不可用
		public static function setToolEnable(id:int,level:int = 1):void{
			var tools:String = JPara.toolEnable;
			var ret:String = "";
			
			for(var i:int = 0 ; i < tools.length ; i++){
				var cc:String = tools.substr(i,1);
				
				if(i == id){
					cc = level + "";
				}
				ret += cc;
			}
			JPara.toolEnable = ret;
		}

		//sys - 新建道具
		public static function newTool():void{
			var tnum:int;
			var pos:int = -1;
			for(var i:int = 0 ; i < JPara.toolEnable.length ; i++){
				if(JPara.toolEnable.substr(i,1) != "0") pos = i;
			}
			pos++;
			
			while(pos != 0){
				tnum = Math.random()*pos;
				if(JPara.toolEnable.substr(tnum,1) == "1"){
					break;
				}
			}
			
			runTool(tnum);
		}
		
		//sys - 使用道具
		public static function runTool(num:int):void{
			JPara.num_tool[num]++;
			
			var efc:MovieClip;
			
			switch(num){
				case TOOL_SEARCH:{
					JTimer.getInstance().timePause();
					JMatrix.getInstance().noHit();
					
					efc = new tSearch();
					efc.mfunc = run_search;
					JMatrix.getInstance().effctStage.addChild(efc);
					break;
				}
				case TOOL_BOMB:{
					JTimer.getInstance().timePause();
					JMatrix.getInstance().noHit();
					
					efc = new tBomb();
					efc.mfunc = run_bomb;
					JMatrix.getInstance().effctStage.addChild(efc);
					break;
				}
				
				//以下为其他道具接口，因为动画对象未定义，暂时注释，以后可以用
				
				/*case TOOL_LIGHTENING:{
					JTimer.getInstance().timePause();
					JMatrix.getInstance().noHit();
					
					efc = new tLightening();
					efc.mfunc = run_lightening;
					JMatrix.getInstance().effctStage.addChild(efc);
					break;
				}
				case TOOL_CUBE:{
					JTimer.getInstance().timePause();
					JMatrix.getInstance().noHit();
					
					efc = new tCube();
					efc.mfunc = run_cube;
					JMatrix.getInstance().effctStage.addChild(efc);
					break;
				}
				case TOOL_MOONLIGHT:{
					JTimer.getInstance().timePause();
					JMatrix.getInstance().noHit();
					
					efc = new tMoonlight();
					efc.mfunc = run_moonlight;
					JMatrix.getInstance().effctStage.addChild(efc);
					break;
				}
				case TOOL_TIME:{
					JTimer.getInstance().timePause();
					JMatrix.getInstance().noHit();
					
					efc = new tTime();
					efc.mfunc = run_time;
					JMatrix.getInstance().effctStage.addChild(efc);
					break;
				}
				case TOOL_PAINTBUCKET:{
					JTimer.getInstance().timePause();
					JMatrix.getInstance().noHit();
					
					efc = new tPaintbucket();
					efc.mfunc = run_paintbucket;
					JMatrix.getInstance().effctStage.addChild(efc);
					break;
				}*/
			}
		}
		
		
		//放大镜效果
		public static function run_search(mc:MovieClip):void{
			mc.stop();
			
			JMatrix.getInstance().effctStage.removeChild(mc);
			
			JPara.m_search.t_num.text++;
			
			JTimer.getInstance().timePlay();
			JTimer.getInstance().refreshComboTime();
			JMatrix.getInstance().goHit();
		}
		
		//爆炸效果
		public static function run_bomb(mc:MovieClip):void{
			mc.stop();
			
			JMatrix.getInstance().effctStage.removeChild(mc);
			
			var mx:int = Math.random()*(JMatrix.getInstance().width - 2) + 1;
			var my:int = Math.random()*(JMatrix.getInstance().height - 2) + 1;
			
			for(var x:int = -1 ; x <= 1 ; x++){
				for(var y:int = -1 ; y <= 1 ; y++){
					var mc:MovieClip = JMatrix.getInstance().getCrystal(mx + x,my + y);
					if(mc != null){
						JPara.addScore(1);
						JAnimation.deleteCrystal(mc,JMatrix.getInstance().deleteCrystal);
					}
				}
			}
			
			JTimer.getInstance().timePlay();
			JTimer.getInstance().refreshComboTime();
			JMatrix.getInstance().goHit();
		}
		
		//闪电效果
		public static function run_lightening(mc:MovieClip):void{
		
		}
		
		//魔方效果
		public static function run_cube(mc:MovieClip):void{
		
		}
		
		//月光效果
		public static function run_moonlight(mc:MovieClip):void{
		
		}
		
		//时间效果
		public static function run_time(mc:MovieClip):void{
		
		}
		
		//油漆桶效果
		public static function run_paintbucket(mc:MovieClip):void{
		
		}
		
	}
}
